package com.mineg.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


@ControllerAdvice
public class ControllerExceptionHandler {

    private final Logger logger=LoggerFactory.getLogger(this.getClass());

    /*
    * 错误拦截器，处理所有的错误
    * */
    @ExceptionHandler({Exception.class})
    public ModelAndView exceptionHandler(HttpServletRequest request,Exception e) throws Exception {
        logger.error("Request URL : {},Exception : {}", request.getRequestURL(),e);
        //有错误则返回null
        System.out.println("==================================="+AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class));
        /*
        * 判断是否有状态码
    * */
//        if(AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) == null){
//            throw e;
//        }

        ModelAndView mav=new ModelAndView();
        mav.addObject("url",request.getRequestURL());
        mav.addObject("exception",e);
        mav.setViewName("error/error");
        return mav;
    }
}
